/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iwords.ejb;

import com.iwords.service.object.LearningStatusEntity;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author sunmellon
 */
@Stateless
public class LearningStatusEntityFacade extends AbstractFacade<LearningStatusEntity> {

    @PersistenceContext(unitName = "iwords")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public LearningStatusEntityFacade() {
        super(LearningStatusEntity.class);
    }

    /**
     * 查询当前词库的状态分布
     * select familiarity, count(*) from learningstatus where userid=2 and libid=348 group by familiarity
     * 查询一个学生的最近背单词的时间
     * select userid, max(updatetime) from learningstatus where userid in (select userid from users where teacherid=3052) group by userid
     */
    public List<Object[]> findLearningStatus(Long teacherid) {
        return getEntityManager()
                .createNativeQuery("select userid, max(updatetime) from learningstatus where userid in (select userid from users where teacherid=?) group by userid")
                .setParameter(1, teacherid)
                .getResultList();
    }
    
    /**
     * 查询学生24小时内学习过的单词
     */
    public List<LearningStatusEntity> findLearningStatusIn24Hours(Long userid) {
        java.util.Date now = new java.util.Date(System.currentTimeMillis() - 24*3600*1000);
        return getEntityManager().createQuery("SELECT u FROM LearningStatusEntity u WHERE u.userid=?1 and u.updatetime>?2")
               .setParameter(1, userid)
                .setParameter(2, now)
                .getResultList();
    }
}
